<template>
  <div class="question-container">
    <el-card>
      <div>胡媛媛</div>
      <!-- 筛选 -->
      <el-form label-width="80px" size="small">
        <el-row>
          <el-col :span="6">
            <el-form-item label="关键字">
              <el-input v-model="requestParams.keyword" placeholder="根据编号搜索"/>
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="12">
            <el-form-item style="text-align:right">
              <el-button @click="clear">清除</el-button>
              <el-button @click="filter" type="primary">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据记录 -->
      <el-alert :title="`数据一共 ${total} 条`" :closable="false" show-icon />
      <!-- 列表 -->
      <el-table :data="questions" style="width: 100%">
        <el-table-column label="编号" prop="id" width="220"></el-table-column>
        <el-table-column label="题型" width="80">
          <template v-slot="{row}">
            {{ questionType.find(item => item.value===(row.questionType-0)).label }}
          </template>
        </el-table-column>
        <el-table-column label="题目编号" width="220">
          <template v-slot="{row}">
            <div v-for="item in row.questionIDs" :key="item.number">
              <a href="#" @click.prevent="openPreviewDialog(item)" style="color:#09f;">{{item.number}}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="录入时间" width="180">
          <template v-slot="{row}">
            {{row.addTime|parseTimeByString}}
          </template>
        </el-table-column>
        <el-table-column label="答题时间(s)" prop="totalSeconds"></el-table-column>
        <el-table-column label="正确率(%)" prop="accuracyRate" ></el-table-column>
        <el-table-column label="录入人" prop="userName"></el-table-column>
        <el-table-column label="操作" width="80px">
          <template v-slot="{row}">
            <el-button plain circle type="danger" size="small" icon="el-icon-delete" @click="delQuestion(row)" title="删除"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row type="flex" justify="end">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="pager"
          :current-page="requestParams.page"
          :page-sizes="[20,30,50,100]"
          :page-size="requestParams.pagesize"
          layout="prev, pager, next, sizes, jumper"
          :total="total">
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 预览 -->
    <QuestionsPreview ref="questionPreview" :data="questionInfo"/>
  </div>
</template>

<script>
import { difficulty, questionType } from '@/api/hmmm/constants'
import { randoms as questionList, removeRandoms as questionDel } from '@/api/hmmm/questions'
import QuestionsPreview from '../components/questions-preview'
export default {
  name: 'QuestionsRandoms',
  components: {
    QuestionsPreview
  },
  data () {
    return {
      questionInfo: {},
      questionType,
      difficulty,
      total: 0,
      questions: [],
      requestParams: {
        keyword: null,
        page: 1,
        pagesize: 20
      }
    }
  },
  async created () {
    this.getList()
  },
  methods: {
    // 获取数据列表
    async getList () {
      const { data: questionsData } = await questionList(this.requestParams)
      this.questions = questionsData.items
      this.total = questionsData.counts
    },
    // 题目 预览
    openPreviewDialog (questionInfo) {
      this.questionInfo = questionInfo
      this.$nextTick(() => {
        this.$refs.questionPreview.open()
      })
    },
    // 删除
    delQuestion (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await questionDel(row)
        this.$message.success('删除成功')
        this.getList()
      }).catch(() => {})
    },
    // 清除
    clear () {
      this.requestParams.keyword = null
      this.getList()
    },
    // 搜索
    filter () {
      this.requestParams.page = 1
      this.getList()
    },
    // 每页显示信息条数
    handleSizeChange (val) {
      this.requestParams.pagesize = val
      this.getList()
    },
    // 进入某一页
    pager (val) {
      this.requestParams.page = val
      this.getList()
    }
  }
}
</script>

<style lang="scss">
.el-card{
  margin: 10px;
  .el-alert{
    margin-bottom: 15px;
  }
  .el-pagination{
    margin-top: 20px;
  }
}
</style>
